package com.carboy.launch.widgets;

import android.app.Dialog;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.carboy.launch.R;
import com.carboy.launch.able.AbstractDialogOperate;
import com.carboy.launch.activity.setting.SHotSpotActivity;
import com.carboy.launch.utils.Tool;

/**
 * wifi连接操作Dialog
 */
public class WifiHotSetDialog extends Dialog {

	private Context mContext;
	private Button hot_cancel_btn; // 取消
	private Button hot_safe_btn; // 连接
	private EditText hot_pwd_edit; // 密码输入框
	private EditText hot_ssid_edit; // ssid
	private CheckBox hot_display_checkbox; // 显示密码
	private RadioGroup radioGroup;
	private RadioButton radioButton1, radioButton2;
	private LinearLayout ll_pwd;

	private boolean isOpen; // 是否开放
	private AbstractDialogOperate absDialogOperate; // Dialog操作抽象类
	private SHotSpotActivity.IHotSpotCallback mIHotSpotCallback;

	public WifiHotSetDialog(Context context, SHotSpotActivity.IHotSpotCallback hotSpotCallback) {
		super(context, R.style.Theme_Light_FullScreenDialogOperate);
		// 默认不显示输入栏，但点击输入框时会弹出来
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		// 绑定Layout
		this.setContentView(R.layout.dialog_wifihot_setting);
		mContext = context;
		this.mIHotSpotCallback = hotSpotCallback;
		// 初始化控件

		hot_cancel_btn = (Button) this.findViewById(R.id.hot_cancel_btn);
		hot_safe_btn = (Button) this.findViewById(R.id.hot_save_btn);
		hot_ssid_edit = (EditText) findViewById(R.id.hot_ssid_edit);
		hot_pwd_edit = (EditText) findViewById(R.id.hot_pwd_edit);
		hot_display_checkbox = (CheckBox) findViewById(R.id.dwc_display_checkbox);
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		radioButton1 = (RadioButton) findViewById(R.id.radiobutton1);
		radioButton2 = (RadioButton) findViewById(R.id.radiobutton2);
		ll_pwd = (LinearLayout) findViewById(R.id.ll_pwd);
		hot_display_checkbox = (CheckBox) findViewById(R.id.hot_display_checkbox);
		initLinistner();
	}

	/**
	 * 设置透明度(针对Dialog)
	 */
	public void setAlpha(float alpha) {
		Window window = WifiHotSetDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = alpha;
		window.setAttributes(lp);
	}

	/**
	 * 设置背景透明度(针对Dialog背景)
	 */
	public void setDimAmount(float dimAmount) {
		Window window = WifiHotSetDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = dimAmount;
		window.setAttributes(lp);
		// 背景模糊
		// window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

	/**
	 * 设置操作抽象类触发
	 */
	public void initLinistner() {

		// 设置点击事件,以及触发操作
		hot_cancel_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		hot_safe_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String pwd = hot_pwd_edit.getText().toString();
				WifiConfiguration wifiConfiguration = new WifiConfiguration();
				WifiManager manager = (WifiManager) mContext
						.getSystemService(Context.WIFI_SERVICE);
				if (hot_ssid_edit.length() <= 0) {
					CustomToast.showToast(mContext, R.string.wifi_SSID_hint);
					return;
				}
				if (radioButton1.isChecked()) {
					if (pwd.length() < 8) {
						CustomToast.showToast(mContext, R.string.wifi_pwd_hint);
						return;
					} else {
						wifiConfiguration.SSID = hot_ssid_edit.getText()
								.toString().trim();
						wifiConfiguration.preSharedKey = pwd;
						wifiConfiguration.allowedAuthAlgorithms
								.set(WifiConfiguration.AuthAlgorithm.OPEN);
						wifiConfiguration.allowedProtocols
								.set(WifiConfiguration.Protocol.RSN);
						wifiConfiguration.allowedProtocols
								.set(WifiConfiguration.Protocol.WPA);
						wifiConfiguration.allowedKeyManagement
								.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
						wifiConfiguration.allowedPairwiseCiphers
								.set(WifiConfiguration.PairwiseCipher.CCMP);
						wifiConfiguration.allowedPairwiseCiphers
								.set(WifiConfiguration.PairwiseCipher.TKIP);
						wifiConfiguration.allowedGroupCiphers
								.set(WifiConfiguration.GroupCipher.CCMP);
						wifiConfiguration.allowedGroupCiphers
								.set(WifiConfiguration.GroupCipher.TKIP);
						Tool.addWifiApEnabled(manager, wifiConfiguration);
					}

				} else {
					wifiConfiguration.SSID = hot_ssid_edit.getText().toString()
							.trim();
					wifiConfiguration.allowedAuthAlgorithms
							.set(WifiConfiguration.AuthAlgorithm.OPEN);
					wifiConfiguration.allowedProtocols
							.set(WifiConfiguration.Protocol.RSN);
					wifiConfiguration.allowedProtocols
							.set(WifiConfiguration.Protocol.WPA);
					wifiConfiguration.allowedKeyManagement
							.set(WifiConfiguration.KeyMgmt.NONE);
					wifiConfiguration.allowedPairwiseCiphers
							.set(WifiConfiguration.PairwiseCipher.CCMP);
					wifiConfiguration.allowedPairwiseCiphers
							.set(WifiConfiguration.PairwiseCipher.TKIP);
					wifiConfiguration.allowedGroupCiphers
							.set(WifiConfiguration.GroupCipher.CCMP);
					wifiConfiguration.allowedGroupCiphers
							.set(WifiConfiguration.GroupCipher.TKIP);
					Tool.addWifiApEnabled(manager, wifiConfiguration);
				}
				hot_pwd_edit.setText("");
				dismiss();
				mIHotSpotCallback.saveHotSet();
			}
		});
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				switch (arg0.getCheckedRadioButtonId()) {
				case R.id.radiobutton1:
					ll_pwd.setVisibility(View.VISIBLE);
					break;
				case R.id.radiobutton2:
					ll_pwd.setVisibility(View.GONE);
					break;
				default:
					break;
				}
			}
		});

		hot_display_checkbox
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) { // 显示密码
							hot_pwd_edit
									.setTransformationMethod(HideReturnsTransformationMethod
											.getInstance());
						} else { // 隐藏密码
							hot_pwd_edit
									.setTransformationMethod(PasswordTransformationMethod
											.getInstance());
						}
						hot_pwd_edit.postInvalidate();
					}
				});
	}

	/**
	 * 显示dialog
	 */
	public void showDialog(WifiConfiguration wifiConfiguration) {
		hot_ssid_edit.setText(wifiConfiguration.SSID);
		if (wifiConfiguration.preSharedKey != null) {
			ll_pwd.setVisibility(View.VISIBLE);
			hot_pwd_edit.setText(wifiConfiguration.preSharedKey);
			radioButton1.setChecked(true);
		} else {
			radioButton2.setChecked(true);
			ll_pwd.setVisibility(View.GONE);
		}
		WifiHotSetDialog.this.show(); // 弹出dialog
	}

	/**
	 * 关闭Dialog
	 */
	public void cancelDialog() {
		if (this.isShowing()) {
			this.cancel();
		}
	}
}
